package com.project.common.pagination.handler;

import com.project.common.pagination.Pagination;
import com.project.common.pagination.PaginationHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;

import java.text.MessageFormat;

public class MySQLPaginationHandler implements PaginationHandler {
    private static final String PAGINATION_SQL_PATTERN = "select * from ({0}) alias_mysql limit {1,number,#},{2,number,#}";

    @Override
    public String paginationStatement(MappedStatement statement,
                                      BoundSql boundSql,
                                      Pagination pagination) {
        String handler = MessageFormat.format(PAGINATION_SQL_PATTERN,
                boundSql.getSql(),
                pagination.from(),
                pagination.getPageSize()
        );
        return handler;
    }
}
